package finbudgettable.client.ui.grid.cell.links;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeUri;
import com.google.gwt.safehtml.shared.UriUtils;

import finbudget.shared.process.events.StringPair;
import finbudgettable.client.ui.grid.cell.HasMultilineStyle;
import finbudgettable.client.ui.grid.cell.link.LinkCell;

public class LinksCell extends AbstractCell<StringPair> implements HasMultilineStyle {

	private boolean multilineStyle;

	@Override
	public boolean isMultilineStyle() {
		return multilineStyle;
	}

	@Override
	public void setMultilineStyle(boolean multilineStyle) {
		this.multilineStyle = multilineStyle;
	}

	@Override
	public void render(Context context, StringPair value, SafeHtmlBuilder sb) {
        if (value != null) {
            String className;
            if (multilineStyle) {
                className = LinkCell.RESOURCES.style().bLinkCell__multiline_text();
            } else {
                className = LinkCell.RESOURCES.style().bLinkCell__text();
            }
            String name = value.getLeft();
            if (name != null) {
                String urlCandidate = value.getRight();
                SafeUri url = UriUtils.fromSafeConstant(urlCandidate);
                sb.append(LinkCell.TEMPLATE.div(className, LinkCell.TEMPLATE.link(url, name)));
            }
        }
	}

}
